const App = getApp()
import WxResource from '../../../../helpers/WxResource'

Page({
	data: {
		show: !0,
		form: {
			linkman: '',
			phone: '',
			address: '',
			areaId: '',
		},

	},
	onLoad() {
		this.WxValidate = App.WxValidate({
			linkman: {
				required: true,
				minlength: 2,
				maxlength: 10,
			},
			phone: {
				required: true,
			},
			address: {
				required: true,
				minlength: 2,
				maxlength: 60,
			},
			area_id: {
				required: true,
			},
		}, {
				linkman: {
					required: '请输入收货人姓名',
				},
				tel: {
					required: '请输入收货人电话',
				},
				address: {
					required: '请输入收货人地址',
				},
				area_id: {
					required: '请选择收货人区域',
				},
			})

		this.address = App.HttpResource('/address/:id', { id: '@id' })
		this.setData({
			isShow: false, // 显示区域选择框
			showDistrict: true // 默认为省市区三级区域选择
		});


		this.area = App.HttpResource('/area/:id', { id: '@id' })
		this.area.queryAsync().then(province => {
			this.setData({
				provinces: province,
			});
			return this.area.getAsync({ id: province[0].id })
		}).then(city=>{
      this.setData({
        citys: city,
      });
			return this.area.getAsync({ id: city[0].id })
		}).then(  county=>{
      this.setData({
        area: [0, 0, 0],
        countys: county,
				'form.area': this.data.provinces[0].name + ' - ' + this.data.citys[0].name + ' - ' +county[0].name,
				'form.area_id': county[0].id,
      });
		});
	},
	submitForm(e) {
		const params = e.detail.value

		if (!this.WxValidate.checkForm(e)) {
			const error = this.WxValidate.errorList[0]
			App.WxService.showModal({
				title: '友情提示',
				content: `${error.msg}`,
				showCancel: !1,
			})
			return false
		}

		// App.HttpService.postAddress(params)
		this.address.saveAsync(params)
			.then(data => {

					this.showToast('添加成功')
			})
	},
	showToast(message) {
		App.WxService.showToast({
			title: message,
			icon: 'success',
			duration: 1500,
		})
			.then(() => App.WxService.navigateBack())
	},

	  bindChange: function(e) {
	    var val = e.detail.value
	    var area = this.data.area;
	    var _data = this.data;

		    if(val[0] != area[0]){
					this.area.getAsync({ id: _data.provinces[val[0]].id }).then(city=>{
			      const firstCity = city[0];
			      this.setData({
			        citys: city
			      });
						return this.area.getAsync({ id: firstCity.id })
					}).then(  county=>{
			      this.setData({
			        area: [val[0], 0, 0],
			        countys: county,
							'form.area': this.data.provinces[val[0]].name + ' - ' + this.data.citys[0].name + ' - ' +county[0].name,
							'form.area_id': county[0].id,
			      });
					});
		      return;
		    }else if(val[1] != area[1]){
					this.area.getAsync({ id: _data.citys[val[1]].id }).then(  county=>{
			      this.setData({
			        area: [val[0], val[1], 0],
			        countys: county,
							'form.area': this.data.provinces[val[0]].name + ' - ' + this.data.citys[val[1]].name + ' - ' +county[0].name,
							'form.area_id': county[0].id,
			      });
					});
		      return;
		    }else  if(val[2] != area[2]){
						this.setData({
							'form.area': this.data.provinces[val[0]].name + ' - ' + this.data.citys[val[1]].name + ' - ' +this.data.countys[val[2]].name,
							'form.area_id': this.data.countys[val[2]].id,
						});
		      return;
		    }

	  },
  open:function(){
    this.setData({
      condition:!this.data.condition
    })
  },
})
